<template lang="jade">
div#app
	div.return-bar(@click="back()")
		i.iconfont &#xe668;
		借款项目


	div.search.cbox.detail_top
		ul.input.w100
			li
				dl.cbox.nptype
					dd(v-bind:class="{ 'on': ltype == ''}",v-on:click="setList('')") 全部
					dd(v-bind:class="{ 'on': ltype == '1'}",v-on:click="setList('1')") 审核中
					dd(v-bind:class="{ 'on': ltype == '2'}",v-on:click="setList('2')") 审核通过

	ul.user-loan-list.top1(v-infinite-scroll="down()" , infinite-scroll-disabled="busy", infinite-scroll-distance="10")
		li(v-for="  (index, item) in items ")
			div.title
				span.fr.time 申请时间:{{item.addtime}}

				i(v-bind:class="  item.borrow_name_no_type=='2' ? ' iconfont red ':' iconfont '  ") {{{getIcon(item.borrow_name_no_type)}}}
				span.txt {{item.name}}
			dl.point.cbox
				dd
					span 借款金额
					b {{item.account}}
				dd
					span 借款利率
					b {{item.borrow_apr}}
				dd
					span 借款期限
					b {{item.borrow_period}}
			div.status(style="padding: 0.25rem 0.2rem 0.35rem;")
				span(v-bind:class="  item.status=='0' ? ' working ':'done '  "){{getStatus(item.status)}}

				span.fr.p_btn(v-if=" item.entrust_pay=='0' ", style="padding: 2px 10px;", @click="doPay(item)") 申请受托支付
				span.fr(v-if=" item.entrust_pay=='1' ", style="padding: 2px 10px;") 受托支付完成

	div.nodata(style="background:none")
		{{msg}}

	form-page(:url.sync="postBank.url", :rdata.sync="postBank.data")
	com-alert(:show.sync="alertData.show", :title.sync="alertData.title", :buttontxt.sync="alertData.buttontxt", :content.sync="alertData.content")
</template>

<style>
@import '~vux/dist/vux.css';
</style>

<script>
var vm = this;
import '../../assets/sass/user/loan.scss'
import {c_user_loan_list} from '../../config/api'
import {Flexbox, FlexboxItem} from 'vux'
import api from '../../data/api';

import FormPage from '../../components/common/form'
import ComAlert from '../../components/common/alert'

export default {

	data () {
		return {
			postBank:{
				url:'',
				data:{
					RequestData:'',
					transCode:''
				}
			},

            alertData:{
                show:false,
                title:'标题',
                buttontxt:'确定',
                content:'sign'
            },

			postForm:{},
			info:{},
			items:[],
			page:1,
			busy:true,
			ltype:'',
			msg:''
		}
	},
	components: {
		ComAlert,
		FormPage,
		Flexbox,
		FlexboxItem
	},
	methods: {
		back(){
			//this.$route.router.go('/auto-buy')
			//window.history.back();
			this.$route.router.go('/loan');
		},

		getForm(){
			for(var a in this.$children){
				if(this.$children[a].name == 'form')
					this.postForm = this.$children[a];
			}
		},

		setList(key){
			this.ltype = key;
			this.items = [];
			this.fetch_data(1)
		},
		getIcon(key){
			var r_val = "";
			if(key=="1")
				r_val = "&#xe640;"
			else
				r_val = "&#xe635;"
			return r_val;
		},

		getStatus(status){
			var val = '';
			switch (status) {

				case '0':
				val = '审核中';
				break;

				case '1':
				val = '借款中';
				break;

				case '3':
				val = '满标放款';
				break;

				case '6':
				val = '撤标';
				break;
			}

			return val;
		},


		down: function() {
			if(this.busy == true || this.items == ''){
				return false;
			}
			this.fetch_data(++this.page);
		},

		async doPay(item){



			this.$store.dispatch('setShowLoading', true), this.busy = true;
			var res = await api.user.get_entruste({'borrow_nid':item.borrow_nid});


			if(res.status){
				this.getForm();
				this.postForm.show = true
				this.postBank.url = res.action_url
				this.postBank.data.RequestData = res.RequestData
				this.postBank.data.transCode = res.transCode
				this.postForm.submit();
			}else{

				this.alertData = {show:true, title:'信息提示', buttontxt:'确定', content:res.msg};

				if(res.code == '00000'){
					setTimeout(function() {
						that.$route.router.go('/user/sign-contract');
						//return;
					}, 2000);
				}
				if(res.msg == '请登录'){
					this.$route.router.go('/login')
				}

			}
			this.$store.dispatch('setShowLoading', false);

		},

		async fetch_data(page) {
			this.$store.dispatch('setShowLoading', true), this.busy = true;
			/* this.$http({url: c_user_loan_list+'&type='+this.ltype+'&page='+page, method: 'GET'}).then(function (response) {
				 this.$store.dispatch('setShowLoading', false);
				var obj = response.data;
				if(!obj.status){
					//alert(obj.msg)
					if(obj.code == '10055'){
						sessionStorage.removeItem("login");
						this.$route.router.go('/login?from='+this.$route.path.substring(1))
					}
					return;
				}

				this.items = this.items.concat(response.data.data.list);
				this.busy = false;
			}); */
			//+'&type='+this.ltype+'&page='+page;
			var data = {'type':this.ltype, 'page':page};
			var res = await api.user.get_loanList(data);
			this.$store.dispatch('setShowLoading', false);
			this.msg = "正在加载数据...!";
			if(res.status){
				this.items = this.items.concat(res.list);
				this.busy = false;
				this.msg = "";
			}else{
				if(res.code == '10055'){
					sessionStorage.removeItem("login");
					this.$route.router.go('/login?from='+this.$route.path.substring(1));
					return;
				}
				this.busy = true;
				this.msg = "没有数据了!";
			}


		}

	},

	route: {
		activate ( transition ) {
			vm = this;
			vm.fetch_data(vm.page);
			transition.next();
			document.title = '借款项目 - 有喜财富';
		}
	}
}
</script>
